package com.luyuan.crm.formula.web;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

import javax.annotation.Resource;
import javax.faces.model.DataModel;

import org.hibernate.criterion.Order;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.luyuan.crm.formula.model.Formula;
import com.luyuan.crm.formula.model.Inventory;
import com.luyuan.crm.formula.service.FormulaService;
import com.luyuan.crm.formula.service.InventoryService;
import com.ruijie.web.jsf.action.BaseAction;
import com.ruijie.web.jsf.context.TableContext;
import com.ruijie.web.jsf.model.PageDataModel;
import com.ruijie.web.jsf.model.TableOrder;
import com.ruijie.web.jsf.model.TablePage;
import com.ruijie.foundation.query.Page;

@Component("addFormulaAction")
@Scope("view")
public class AddFormulaAction extends BaseAction {

    @Resource(name = "formulaService")
    private FormulaService formulaService;

    @Resource(name = "inventoryService")
    private InventoryService inventoryService;

    private List<Inventory> inventoryList = new ArrayList<Inventory>();

    private Inventory inventory;

    private Formula formula;

    public DataModel getProducts() {
        TablePage limit = TableContext.getTablePage();
        List<TableOrder> tableOrders = TableContext.getTableOrders();
        List<Order> orders = new ArrayList<Order>();
        for (TableOrder tableOrder : tableOrders) {
            Order order = tableOrder.toOrder();
            if (order != null) {
                orders.add(order);
            }
        }
        inventory = (Inventory) getRequestAttribute("inventory");
        if (inventory != null) {
            inventoryList.add(inventory);
        }
        Page page = new Page(limit.getPage(), inventoryList.size(), limit.getPageSize(), inventoryList);
        return new PageDataModel(page);
    }

    public Formula getFormula() {
        String formulaId = getRequestParameter("formulaId");
        if (formulaId != null && !"".equals(formulaId)) {
            formula = formulaService.get(formulaId);
            inventoryList.clear();
            inventoryList.addAll(formula.getInventorys());

            Collections.sort(inventoryList);
        }
        if (formula == null) {
            formula = new Formula();
        }
        return formula;
    }

    public void deleteInventory(String productId) {
        for (Inventory inventory : inventoryList) {
            if (inventory.getProduct().getId().equals(productId)) {
                inventoryList.remove(inventory);
                if (inventory.getId() != null) {
                    inventoryService.deleteInventory(inventory);
                }
                break;
            }
        }
    }

    public void addFormula() {
        formula.setCreateTime(new Date());
        formula.setInventorys(new HashSet<Inventory>(inventoryList));
        formulaService.saveFormula(formula);
        formula = new Formula();
        inventoryList.clear();
        addGlobalI18nMessage("成功添加配方信息");
    }

    public void updateFormula() {
        // formula.setProductType(productType);
        formula.setInventorys(new HashSet<Inventory>(inventoryList));
        formulaService.updateFormula(formula);
        addGlobalI18nMessage("修改成功");
    }

    public void setFormula(Formula formula) {
        this.formula = formula;
    }
}
